home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3515 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: division problem
  5. Date: 29 Jan 1996 17:43:05 GMT
  6. Organization: OpenVision
  7. Message-ID: <4ej0v9$rfo@spanky.pls.ov.com>
  8. References: <31097D77.11AA@rain.org>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 11AA@rain.org, tpaul <tpaul@rain.org> writes:
  13. >Can anyone show me why this does not work?  I am a beginner.
  14. >
  15. >#include <stdio.h>
  16. >
  17. >main ()
  18. >{
  19. >    int fahrenheit, celsius;
  20. >    
  21. >    printf("Fahrenheit temperature =?";
  22. >    scanf("%d",fahrenheit);
  23. >    celsius = 5/9*(fahrenheit-32);
  24. >    printf("Fahrenheit = %d, Celsius = %d", fahrenheit, celsius;
  25. >}
  26. >
  27. >
  28. >Thanks in advance.
  29.  
  30.  
  31. First, * and / have the same precedence, so in all probability, the 
  32. expression in the parens is evaluated first, then you get the rest of
  33. the expression.
  34.  
  35. Second, integer divide truncates.  Therefore 5/9 = 0.  If you want to
  36. stick to integers, then write the following expression:
  37.  
  38. celsius = (5 * (fahrenheit - 32)) / 9;
  39.  
  40. This expression will give you a better answer, but not as good an
  41. answer as you would get from using floating point numbers.
  42.  
  43.             Fletcher.Glenn@ov.com
  44.  
  45.